var _api = _interopRequireDefault(require("../../../../utils/api")), _util = _interopRequireDefault(require("../../../../public/utils/util"));

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function _objectSpread(e) {
    for (var t = 1; t < arguments.length; t++) {
        var i = null != arguments[t] ? arguments[t] : {}, a = Object.keys(i);
        "function" == typeof Object.getOwnPropertySymbols && (a = a.concat(Object.getOwnPropertySymbols(i).filter(function(t) {
            return Object.getOwnPropertyDescriptor(i, t).enumerable;
        }))), a.forEach(function(t) {
            _defineProperty(e, t, i[t]);
        });
    }
    return e;
}

function _toConsumableArray(t) {
    return _arrayWithoutHoles(t) || _iterableToArray(t) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(t) {
    if (Symbol.iterator in Object(t) || "[object Arguments]" === Object.prototype.toString.call(t)) return Array.from(t);
}

function _arrayWithoutHoles(t) {
    if (Array.isArray(t)) {
        for (var e = 0, i = new Array(t.length); e < t.length; e++) i[e] = t[e];
        return i;
    }
}

function _defineProperty(t, e, i) {
    return e in t ? Object.defineProperty(t, e, {
        value: i,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = i, t;
}

var MINE__CLIENT = "my", LIMIT = 20, TURNOVER_PLAN = [ {
    id: 1,
    name: "<30%"
}, {
    id: 2,
    name: "≥30%"
}, {
    id: 3,
    name: "≥60%"
}, {
    id: 4,
    name: "≥90%"
}, {
    id: 5,
    name: "100%"
} ], TRANSFORM_TIME = [ {
    id: 1,
    name: "全部"
}, {
    id: 2,
    name: "今日"
}, {
    id: 3,
    name: "本周"
}, {
    id: 4,
    name: "本月"
}, {
    id: 5,
    name: "近半年"
} ];

Component({
    properties: {
        guid: String,
        cardid: String,
        isMember: Boolean,
        isMasses: Boolean,
        BAR_HEIGHT: String
    },
    data: {
        condition: {
            shown: !1,
            filter: !1
        },
        conditionVal: null,
        time: {
            shown: !1,
            filter: !1,
            TRANSFORM_TIME: TRANSFORM_TIME,
            value: 1,
            start: "",
            end: ""
        },
        nicheLabel: {},
        keyword: ""
    },
    ready: function() {
        this.initEvent(), this.init();
    },
    methods: {
        init: function() {
            this.setContactData(this.$appData.contact), this.initData(), this.timeChangeSet(TRANSFORM_TIME[0].id), 
            this.loadNichetList(!0);
        },
        triggerSearch: function(t) {
            var e = t.detail, i = e.inputShowed, a = e.keyword;
            this.setData({
                inputShowed: i,
                keyword: a
            }), this.initSearch && (this.loadNichetList(!0), this.initSearch = !1);
        },
        onSearch: function(t) {
            var e = t.detail.value;
            this.setData({
                keyword: e
            }), this.resetCondition({}, !1), this.resetTime({}, !1), this.initSearch = !0, this.loadNichetList(!0, e);
        },
        triggerCondition: function() {
            var t = !this.data.condition.shown;
            t && this.getNicheLabel(), this.setData({
                "condition.shown": t
            });
        },
        resetCondition: function(t) {
            var e = this, i = !(1 < arguments.length && void 0 !== arguments[1]) || arguments[1], a = this.data.nicheLabel.labels;
            a && (a.forEach(function(t) {
                return t.selected = !1;
            }), this.lastLabel = null, this.setData({
                conditionVal: null,
                "condition.shown": !1,
                "condition.filter": !1,
                "nicheLabel.labels": a
            }, function() {
                return i && e.loadNichetList(!0);
            }));
        },
        confirmCondition: function() {
            var t = this.data.conditionVal;
            this.setData({
                "condition.shown": !1,
                "condition.filter": !0
            }), this.resetTime({}, !1), this.lastLabel = _util.default.deepClone(t), this.loadNichetList(!0);
        },
        selectCondition: function(t) {
            var e = t.detail, i = Object.keys(e)[0];
            this.setData(_defineProperty({}, "conditionVal.".concat(i), +e[i]));
        },
        selectMore: function(t) {
            var e = t.detail.option, i = this.data.conditionVal || {};
            i.labelids = e.filter(function(t) {
                return t.selected;
            }).map(function(t) {
                return t.id;
            }), i.type || i.status || i.ratetype || i.labelids.length || (i = null), this.setData({
                conditionVal: i,
                "nicheLabel.labels": e
            });
        },
        triggerTime: function() {
            this.setData({
                "time.shown": !this.data.time.shown
            });
        },
        onTimeChange: function(t) {
            var e = t.detail.value, i = t.currentTarget.dataset.name;
            this.setData(_defineProperty({}, "time.".concat(i), e));
        },
        resetTime: function(t) {
            var e = this, i = !(1 < arguments.length && void 0 !== arguments[1]) || arguments[1];
            this.timeChangeSet(TRANSFORM_TIME[0].id), this.setData({
                "time.filter": !1,
                "time.shown": !1
            }, function() {
                return i && e.loadNichetList(!0);
            });
        },
        confirmTime: function() {
            var t = this;
            this.resetCondition({}, !1), this.setData({
                "time.shown": !1,
                "time.filter": !0
            }, function() {
                return t.loadNichetList(!0);
            });
        },
        selectTime: function(t) {
            var e = t.detail;
            this.timeChangeSet(e.id);
        },
        timeChangeSet: function(t) {
            var e, i = new Date(), a = this.getDate(i), n = a;
            switch (t) {
              case 1:
                e = "2018-01-01";
                break;

              case 2:
                e = a;
                break;

              case 3:
                i.setDate(i.getDate() - [ 1, 2, 3, 4, 5, 6, 0 ].indexOf(i.getDay())), e = this.getDate(i);
                break;

              case 4:
                e = a.replace(/\d\d$/, "01");
                break;

              case 5:
                i.setMonth(i.getMonth() - 6), e = this.getDate(i);
            }
            this.setData({
                "time.value": t,
                "time.start": e,
                "time.end": n
            });
        },
        getDate: function(t) {
            var e = t.getFullYear(), i = _util.default.formatNumber(t.getMonth() + 1), a = _util.default.formatNumber(t.getDate());
            return "".concat(e, "-").concat(i, "-").concat(a);
        },
        getNicheLabel: function() {
            var i = this;
            if (!this.labelPromise) {
                var t = this.data.guid;
                this.labelPromise = _api.default.minicard.businesslabel.view({
                    data: {
                        guid: t
                    }
                }).then(function(t) {
                    var e = t.data;
                    e.turnover = TURNOVER_PLAN, i.setData({
                        nicheLabel: e
                    });
                });
            }
        },
        initEvent: function() {
            var e = this;
            [ [ "niche.list.change", function() {
                return e.loadNichetList(!0);
            } ], [ "niche.lists.label.update", function() {
                e.loadNichetList(!0), e.labelPromise = null, e.getNicheLabel();
            } ], [ "contact.change", this.setContactData.bind(this) ], [ "niche.department.change", function(t) {
                e.setData({
                    departmentNiche: t
                }), e.loadNichetList(!0);
            } ] ].forEach(function(t) {
                return e.$addListener.apply(e, _toConsumableArray(t));
            });
        },
        initData: function() {
            this.nicheOffset = 0, this.nicheNoMore = !0, this.lastLabel = null, this.setData({
                nicheLists: [],
                nicheGroup: [],
                departmentNiche: {
                    catid: MINE__CLIENT,
                    name: "我的客户"
                }
            });
        },
        setContactData: function(t) {
            var e = t.contact;
            this.setData({
                isMember: e.isMember
            });
        },
        onPullDownRefresh: function() {
            this.loadNichetList(!0, this.data.keyword);
        },
        onReachBottom: function() {
            this.nicheNoMore || this.loadNichetList();
        },
        nicheReset: function() {
            var t = this.data, e = t.label, i = t.condition, a = t.time, n = t.inputShowed, s = t.keyword;
            this.loadNichetList && (e || i.shown || a.shown || n || s) && (this.lastLabel = null, 
            this.setData({
                label: null,
                keyword: "",
                inputShowed: !1
            }), this.resetCondition({}, !1), this.resetTime({}, !1), this.loadNichetList(!0), 
            this.resetSearch());
        },
        getLoadNicheData: function() {
            var t = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : this.data.keyword, e = this.data, i = e.conditionVal, a = e.guid, n = e.time, s = _objectSpread({}, i, {
                keyword: t,
                guid: a
            }), o = "";
            return i && i.labelids && (o = i.labelids.join(",")), s.labelids = o, n.filter && (s.starttime = n.start, 
            s.endtime = n.end), s;
        },
        loadNichetList: function(n) {
            var s = this, t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "";
            n && (this.nicheOffset = 0);
            var e = this.data, i = e.cardid, a = e.departmentNiche, o = (a = void 0 === a ? {} : a).catid;
            return this.hasCat = o !== MINE__CLIENT, _api.default.minicard.business.lists({
                data: _objectSpread({}, this.getLoadNicheData(t), {
                    cardid: i,
                    limit: LIMIT,
                    withcustomer: 0,
                    offset: this.nicheOffset,
                    catid: this.hasCat ? o : ""
                }),
                complete: wx.stopPullDownRefresh
            }).then(function(t) {
                var e = t.data, i = e.list, a = e.businesscount;
                s.hasCat ? s.handleLoadUnderNicheList(n, i, a) : s.handleLoadNicheList(n, i);
            });
        },
        handleLoadUnderNicheList: function(t, e) {
            var i = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : "";
            e.forEach(function(t) {
                return t.isOpen = !1;
            }), this.handleReusltList(t, e, "nicheGroup", i), this.lastOpenNicheList = 0;
        },
        handleLoadNicheList: function(t, e) {
            e = this.handleListTime(e), this.handleReusltList(t, e, "nicheLists", "");
        },
        handleReusltList: function(t, e, i, a) {
            if (t) {
                var n;
                this.setData((_defineProperty(n = {
                    businesscount: a
                }, i, e), _defineProperty(n, "hideTopMenu", this.checkTopMenuVisable(e)), _defineProperty(n, "nicheGroup" === i ? "nicheLists" : "nicheGroup", []), 
                n));
            } else {
                var s = (this.data[i] || {}).length, o = {};
                e.forEach(function(t, e) {
                    o["".concat(i, "[").concat(s + e, "]")] = t;
                }), this.setData(o);
            }
            this.nicheOffset += LIMIT, this.nicheNoMore = e.length < LIMIT;
        },
        handleListTime: function() {
            return (0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : []).map(function(t) {
                return t.time = _util.default.formatTimeStamp(t.updatetime), t;
            });
        },
        checkTopMenuVisable: function(t) {
            var e = this.data, i = e.keyword, a = e.conditionVal, n = e.time;
            return !(i || a || n.filter || t.length || this.hasCat);
        },
        toggleLists: function(t) {
            var e = t.currentTarget.dataset.index, i = this.data.nicheGroup[e], a = "nicheGroup[".concat(e, "]"), n = this.lastOpenIndex, s = {};
            isNaN(n) || e === n || (s["nicheGroup[".concat(n, "].isOpen")] = !1), s["".concat(a, ".isOpen")] = !i.isOpen, 
            this.setData(s), this.lastOpenIndex = e, i.list || this.getNicheSubList(i.cardid, e);
        },
        getNicheSubList: function(t, a) {
            var n = this;
            return _api.default.minicard.business.getcardbusinesslist({
                data: _objectSpread({
                    cardid: t
                }, this.getLoadNicheData())
            }).then(function(t) {
                var e = t.data, i = n.handleListTime(e);
                n.setData(_defineProperty({}, "nicheGroup[".concat(a, "].list"), i));
            });
        },
        viewDetail: function(t) {
            var e = t.currentTarget.dataset, i = e.guid, a = e.touid, n = e.cardid, s = e.fromuid;
            this.$route("/pages/radar/nichedetail/nichedetail", {
                guid: i,
                touid: a,
                cardid: n,
                fromuid: s || ""
            });
        },
        onCellSwipe: function(t) {
            var e = this, i = t.detail.index;
            wx.showModal({
                content: "确认删除该客户？",
                confirmColor: "#e94f4f",
                confirmText: "删除",
                success: function(t) {
                    t.confirm && e.handleDeleteNiche(i);
                }
            });
        },
        handleDeleteNiche: function(a) {
            var n = this, t = this.data, e = t.guid, i = t.cardid, s = t.nicheLists, o = s[a].touid;
            _api.default.minicard.business.delete({
                loading: "删除中...",
                data: {
                    touid: o,
                    guid: e,
                    cardid: i
                }
            }).then(function() {
                var t = n.data, e = t.keyword, i = t.label;
                wx.showToast({
                    title: "删除成功"
                }), s.splice(a, 1), n.setData({
                    nicheLists: s,
                    hideTopMenu: !e && !i && !s.length
                });
            });
        },
        resetSearch: function() {
            this.search = this.search || (this.search = this.selectComponent("#search")), this.search.setData({
                keyword: "",
                inputShowed: !1
            });
        },
        toChatList: function() {
            this.$emit("radar.navbar.change", 1);
        },
        selectNicheScope: function() {
            var t = this.data, e = t.guid, i = t.cardid, a = t.departmentNiche, n = a.catid, s = a.isAllCat, o = t.isMasses, r = t.isMember;
            o || wx.navigateTo({
                url: "/pages/radar/department/department?type=niche&guid=".concat(e, "&ishigher=").concat(r ? 1 : 0, "&cardid=").concat(i, "&catid=").concat(s ? "0" : n)
            });
        }
    }
});